package cn.roisrosy.middleware.db.router.dynamic;

import cn.roisrosy.middleware.db.router.DBContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/***
 *  动态数据源获取，每当切换数据源，都要从这个里面获取
 */
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Value("${router.jdbc.datasource.default}")
    private String defaultDataSource;
    @Override
    protected Object determineCurrentLookupKey() {
        if(StringUtils.isBlank(DBContextHolder.getDbKey())){
            return  defaultDataSource;
        }else {
            return "db"+ DBContextHolder.getDbKey();
        }
    }
}
